<!--终审结果-->
<template>
  <div>
    <el-form :model="checkForm" label-width="100px" class="demo-ruleForm" ref="checkForm" :disabled="formDisabled">
      <el-form-item label="办理结果" prop="approveStatus" :rules="{required: true, message: '请选择办理结果'}">
        <el-radio-group v-model="checkForm.approveStatus"  @change="handleChange" >
          <el-radio :label="5" class="red">拒件</el-radio>
          <el-radio :label="3" class="red">退件</el-radio>
          <el-radio :label="9" class="red">通过</el-radio>
        </el-radio-group>
      </el-form-item>

      <div v-if="approveStatusPass">
        <el-form-item label="批复金额" prop="approvedAmount" :rules="{required: true, message: '请输入金额'}">
          <el-input :rows="7" v-model="checkForm.approvedAmount" style="width: 300px" placeholder="请输入金额"><template slot="append">元</template></el-input>
        </el-form-item>
        <el-form-item label="年利率" prop="interestRate" :rules="{required: true, message: '请输入年利率'}">
          <el-input :rows="7" v-model="checkForm.interestRate" style="width: 300px" placeholder="年利率"><template slot="append">%</template></el-input>
        </el-form-item>
        <el-form-item label="还款期限" prop="months" style="width: 400px;" :rules="{required: true, message: '请选择还款期限'}">
          <el-select v-model="checkForm.months" placeholder="请选择" style="width:100%;" ref="loanTermSelect">
            <el-option
              v-for="item in loanTerms"
              :key="item.dictValue"
              :label="item.dictName"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="还款方式" prop="repayMethod" style="width: 400px;" :rules="{required: true, message: '请选择还款方式'}">
          <el-select v-model="checkForm.repayMethod" placeholder="请选择" style="width:100%;">
            <el-option
              v-for="item in repayMethod"
              :key="item.dictValue"
              :label="item.dictName"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
      </div>

      <el-form-item label="说明" prop="opinion">
        <el-input type="textarea" :rows="7" v-model="checkForm.opinion" style="width: 300px" placeholder="请输入说明，最多可输入100字" maxlength="100" show-word-limit/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('checkForm')" v-loading="loading" v-if="isTaskProcessing">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>

  import { finalApproval, getReviewByTaskId } from '@/api/creditLoan'
import DicSelect from '@/components/select/index';
import { listDict ,findByDictTypeName} from '@/api/dict'
export default {
  props: {
    projectId: undefined,
    isTaskProcessing: undefined,
    taskId: undefined,
    currentNodeProjectStatus: undefined   // 当前节点的项目状态
  },
  components: { DicSelect },
  data() {
    return {
      checkForm: {
        approveStatus: undefined,
        opinion: undefined,
        projectId: undefined,
        taskId: undefined
      },
      loading: false,
      approveStatusPass: false,
      formDisabled: false,
      repayMethod:[],
      loanTerms:[]
    };
  },

  created: function () {
    this.taskId = this.$route.query.taskId;
    this.getList()
    if (this.isTaskProcessing === undefined){
      this.isTaskProcessing = false;
    }
    if (this.isTaskProcessing == false){
      this.getReviewByTaskId();
      this.formDisabled = true;
    }
    this.getLoanTermDict('申请期限');
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.checkForm.projectId = this.projectId;
          this.checkForm.taskId = this.taskId;
          this.checkForm.loanTermName = this.$refs.loanTermSelect.selected.label;
          this.$confirm('你确定要提交吗?', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',}).then(() => {
              // 开始提交信息
              this.loading = true;
              finalApproval(this.checkForm).then((data) => {
                this.loading = false;
                this.$notify.success({title: '成功',message: '操作成功'})
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.go(-1);
              }).catch(() => {
                this.loading = false
              })
            })
        } else {
          return false
        }
      })
    },

    // 查询当审批信息
    getReviewByTaskId() {
      this.loading = true;
      getReviewByTaskId(this.taskId).then((data) => {
        this.checkForm=data.data
        if (data.data.approveStatus === 9){
          this.approveStatusPass = true;
        }
      });
      this.loading = false;
    },

    handleChange(node) {
      this.$refs['checkForm'].resetFields() // 重置表单
      this.checkForm.approveStatus = node
      this.approveStatusPass = false;
      if (node == 9){
        this.approveStatusPass = true;
      }
    },
    getList() {
      findByDictTypeName("还款方式")
        .then(response => {
          let list = response.data.filter(item => item.dictName == "等额本息")
          this.repayMethod = list
        })
        .catch(() => {
          this.repayMethod = []
        })
    },
    getLoanTermDict(param) {
      findByDictTypeName(param).then(res => {
          this.loanTerms = res.data
        }).catch(() => {
        this.loanTerms = []
        })
    },
  }
}
</script>
